using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.DomainEntities.PurQualificationAudit.DomainService;
using SOEI.Solution.DomainEntities.PurQualificationAudit.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.PurQualificationAudit
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = true)]
    public class PurFamilyMemberAppService : SolutionAppServiceBase, IPurFamilyMemberAppService
    {
        /// <summary>
        ///【PurFamilyMember】仓储层
        /// </summary>
        private readonly IRepository<PurFamilyMember, string> _purfamilymemberRepository;

        /// <summary>
        ///【PurFamilyMember】领域服务
        /// </summary>
        private readonly IPurFamilyMemberManager _purfamilymemberManager;

        public PurFamilyMemberAppService(
            IRepository<PurFamilyMember, string> purfamilymemberRepository,
            IPurFamilyMemberManager purfamilymemberManager
        )
        {
            _purfamilymemberRepository = purfamilymemberRepository;
            _purfamilymemberManager = purfamilymemberManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【PurFamilyMember】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<PurFamilyMemberListDto>> GetPaged(GetPurFamilyMembersInput input)
        {
            var query = _purfamilymemberRepository.GetAll()
                          //模糊搜索 字段Id
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Id.Contains(input.FilterText))
                          //模糊搜索 字段GFZGSCSQBH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.GFZGSCSQBH.Contains(input.FilterText))
                          //模糊搜索 字段XM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.XM.Contains(input.FilterText))
                          //模糊搜索 字段JTCYGXDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.JTCYGXDM.Contains(input.FilterText))
                          //模糊搜索 字段ZJLXDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZJLXDM.Contains(input.FilterText))
                          //模糊搜索 字段ZJHM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ZJHM.Contains(input.FilterText))
                          //模糊搜索 字段HKLXDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.HKLXDM.Contains(input.FilterText))
                          //模糊搜索 字段SFGFRDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SFGFRDM.Contains(input.FilterText))
                          //模糊搜索 字段SM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.SM.Contains(input.FilterText))
                          //模糊搜索 字段WSQRDM
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.WSQRDM.Contains(input.FilterText))
                          //模糊搜索 字段LXDH
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.LXDH.Contains(input.FilterText))
                          //模糊搜索 字段LXDZ
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.LXDZ.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<PurFamilyMemberListDto>>(entityList);

            return new PagedResultDto<PurFamilyMemberListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【PurFamilyMember】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<PurFamilyMemberListDto> GetById(EntityDto<string> input)
        {
            var entity = await _purfamilymemberRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<PurFamilyMemberListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【Company】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetPurFamilyMemberForEditOutput> GetForEdit(EntityDto<string> input)
        {
            var output = new GetPurFamilyMemberForEditOutput();
            PurFamilyMemberEditDto editDto;

            if (!string.IsNullOrEmpty(input.Id))
            {
                var entity = await _purfamilymemberRepository.GetAsync(input.Id);
                editDto = ObjectMapper.Map<PurFamilyMemberEditDto>(entity);
            }
            else
            {
                editDto = new PurFamilyMemberEditDto();
            }
            output.PurFamilyMember = editDto;
            return output;
        }
        /// <summary>
        ///【PurFamilyMember】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdatePurFamilyMemberInput input)
        {
            if (!string.IsNullOrEmpty(input.PurFamilyMember.Id))
            {
                await Update(input.PurFamilyMember);
            }
            else
            {
                await Create(input.PurFamilyMember);
            }
        }
        /// <summary>
        ///【PurFamilyMember】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<PurFamilyMemberEditDto> Create(PurFamilyMemberEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<PurFamilyMember>(input);
            //调用领域服务
            entity = await _purfamilymemberManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<PurFamilyMemberEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【PurFamilyMember】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(PurFamilyMemberEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id;
            var entity = await _purfamilymemberRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _purfamilymemberManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【PurFamilyMember】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<string> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _purfamilymemberManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【PurFamilyMember】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<string> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _purfamilymemberManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/
        #endregion
    }
}
